package main;

import com.kcbsdk.CSvrDevice;
import com.kcbsdk.EventHandler;
import com.kcbsdk.wpapper;
import com.kcbsdk.MyDevice.AAmplifyWork;
import com.kcbsdk.MyDevice.AAudioWork;
import com.kcbsdk.MyDevice.ABindInfo;
import com.kcbsdk.MyDevice.ADeviceInfo;
import com.kcbsdk.MyDevice.AMediaWork;
import com.kcbsdk.MyDevice.ARdWrFileStatus;
import com.kcbsdk.MyDevice.AShareAdmin;
import com.kcbsdk.MyDevice.ASysRegister;
import com.kcbsdk.MyDevice.AWifiList;

import android.app.Activity;
import android.util.Log;
import android.widget.Toast;


public class MyDevice implements wpapper {
    public CSvrDevice mSvrDevice;
    public Activity mActivity = null;
    public EventHandler mEventHandler = null;

    public int gDevNumber;
    public ADeviceInfo mDbsDevInfo[];
    public ADeviceInfo mFindDevInfo[];
    public AWifiList mAWifiList[];

    public ABindInfo mABindInfo[];
    public int gABindInfoQty;
    public AShareAdmin mAShareAdmin[];
    public int gAShareAdminQty;
    public String gsLyricText[];
//    public char gLyricColor[5][512];

    public int g4LinkMode;
    public String gsDebugText;
    public ARdWrFileStatus mARdWrFileStatus;

    public ASysRegister mASysRegister;
    public AAudioWork mAAudioWork;
    public AMediaWork mAMediaWork;
    public AAmplifyWork mAAmplifyWork;

    private long g4SaveTickCounter;		// 10秒更新一次播放时间，需要自己完善秒显示
    private int g4AddCounter;
    private int gAutoCountTime;

    public MyDevice(){
        mSvrDevice = new CSvrDevice();
        mDbsDevInfo = new ADeviceInfo[32];
        mFindDevInfo = new ADeviceInfo[32];
        mAWifiList = new AWifiList[32];
        mABindInfo = new ABindInfo[8];
        mAShareAdmin = new AShareAdmin[3];
        gsLyricText = new String [5];
        mASysRegister = new ASysRegister();
        mAAudioWork = new AAudioWork();
        mAMediaWork = new AMediaWork();
        mAAmplifyWork = new AAmplifyWork();
    }
    public void pollingTimer(){
        int methodType;
        for (int counter = 0; counter < 8; counter++){
            methodType = mSvrDevice.getMethodType();
            if (methodType > 0){
//                    MSTRING("mSvrDevice.getMethodType " + String.format("%08X", methodType));
                String message = mSvrDevice.getMethodMessage(methodType);
                boolean isFail = ("ok".equals(message)) ? false : true;
                mEventHandler.onComplete(methodType, isFail, message);
            }
            else {
                break;
            }
        }
        int newEvent = mSvrDevice.pollingEvent(gDevNumber);
        if (newEvent > 0){
            if ((newEvent & DEVICE_EVENT_DBS_DEV_INFO) > 0){
                mDbsDevInfo = mSvrDevice.eventDbsDevInfo(gDevNumber, newEvent);
            }
            if ((newEvent & DEVICE_EVENT_FIND_DEV_INFO) > 0){
                mFindDevInfo = mSvrDevice.eventFindDevInfo(newEvent);
            }
            if ((newEvent & DEVICE_EVENT_WIFI_LIST) > 0){
                mAWifiList = mSvrDevice.eventWifiList(gDevNumber, newEvent);
            }
            if ((newEvent & DEVICE_EVENT_LINK_MODE) > 0){
                g4LinkMode = mSvrDevice.eventLinkMode(gDevNumber, newEvent);
            }
            if ((newEvent & DEVICE_EVENT_DEBUG_TEXT) > 0){
                gsDebugText = mSvrDevice.eventDebugText(gDevNumber, newEvent);
            }
            if ((newEvent & DEVICE_EVENT_RD_FILE) > 0){
                mARdWrFileStatus = mSvrDevice.eventReadFile(gDevNumber, newEvent);
            }
            if ((newEvent & DEVICE_EVENT_WR_FILE) > 0){
                mARdWrFileStatus = mSvrDevice.eventWriteFile(gDevNumber, newEvent);
            }
            if ((newEvent & DEVICE_EVENT_RTC_TEXT) > 0){
                gsDebugText = mSvrDevice.eventRtcText(gDevNumber, newEvent);
            }
            if ((newEvent & DEVICE_EVENT_BIND_INFO) > 0){
                mABindInfo = mSvrDevice.eventBindInfo(gDevNumber, newEvent);
            }
            if ((newEvent & DEVICE_EVENT_SHARE_ADMIN) > 0){
                mAShareAdmin = mSvrDevice.eventShareAdmin(gDevNumber, newEvent);
            }
            if ((newEvent & DEVICE_EVENT_REGISTER_ALL) > 0) {
                int g4Local_Buffer[] = new int[32];
                int regNum = mSvrDevice.ReadRegister(gDevNumber, newEvent, g4Local_Buffer, DEVICE_REG_HOST_SYS);
                if (regNum >= 0) {
                    mASysRegister = mSvrDevice.eventSysRegister(gDevNumber, regNum, g4Local_Buffer);
                }
                regNum = mSvrDevice.ReadRegister(gDevNumber, newEvent, g4Local_Buffer, DEVICE_REG_AUDIO_WORK);
                if (regNum >= 0) {
                    mAAudioWork = mSvrDevice.eventAudioWork(gDevNumber, regNum, g4Local_Buffer);
                    if (mAAudioWork.isPlaying) {
                        g4SaveTickCounter = System.currentTimeMillis();        // 10秒更新一次播放时间，需要自己完善秒显示
                        g4AddCounter = 1000;
                        gAutoCountTime = 12;
                    }
                }
                regNum = mSvrDevice.ReadRegister(gDevNumber, newEvent, g4Local_Buffer, DEVICE_REG_MEDIA_WORK);
                if (regNum >= 0) {
                    mAMediaWork = mSvrDevice.eventMediaWork(gDevNumber, regNum, g4Local_Buffer);
                }
                regNum = mSvrDevice.ReadRegister(gDevNumber, newEvent, g4Local_Buffer, DEVICE_REG_AMPLIFY_WORK);
                if (regNum >= 0) {
                    mAAmplifyWork = mSvrDevice.eventAmplifyWork(gDevNumber, regNum, g4Local_Buffer);
                }
            }
            mEventHandler.onComplete(0x00000000, false, "ok");
        }

        if (gAutoCountTime > 0 && mAAudioWork.isPlaying){
            if ((System.currentTimeMillis() - g4SaveTickCounter) > g4AddCounter){
//MDEBUG(0xee);MSYS_DEC(GetTickCount() - mSYS_AAudioWork.g4SaveTickCounter);
                g4AddCounter += 1000;
                mAAudioWork.PlayTime += 1;
//MSYS_DEC(mSYS_AAudioWork.g2PlayTime);
                gAutoCountTime -= 1;
                //	MShowPlayTime();
                newEvent |= 0x00020000;
                mEventHandler.onComplete(0x00000000, false, "ok");
            }
        }
    }
    public void Toast(String text){
        Toast.makeText(mActivity, text, Toast.LENGTH_LONG).show();
        Log.d("Device", text);
    }
    public void Log(String text){
        Log.d("Device", text);
    }
    public void  setUiEventCallBack(Activity activity, EventHandler handler){
        mActivity = activity;
        mSvrDevice.setMethodType();
        mEventHandler = handler;
        MREG.g4LastSelect = tDEVICE;
    }
    public void getLocalDevInfoDbs(){
        mSvrDevice.setDevInfoDbs(0, 0, "");
    }
    public void addLocalDevInfoDbs(String macAddress){
        gDevNumber = mSvrDevice.setDevInfoDbs(1, 0, macAddress);
        mSvrDevice.cancelAndRestart(gDevNumber, DEVICE_EVENT_LINK_MODE | DEVICE_EVENT_BIND_INFO | DEVICE_EVENT_REGISTER_ALL, 0);
    }
    public void selectLocalDevInfoDbs(String macAddress){
        gDevNumber = mSvrDevice.setDevInfoDbs(2, 0, macAddress);
        mSvrDevice.cancelAndRestart(gDevNumber, DEVICE_EVENT_LINK_MODE | DEVICE_EVENT_BIND_INFO | DEVICE_EVENT_REGISTER_ALL, 0);
    }
    public void deleteLocalDevInfoDbs(String macAddress){
        mSvrDevice.setDevInfoDbs(3, 0, macAddress);
    }
    public void findLocalDevive(boolean network, boolean emulation){
        int type = 0;
        if (network){
            type |= 0x00010000;
        }
        if (emulation){
            type |= 0x00020000;
        }
        mSvrDevice.FindDevInfo(type, 0, "", "");
    }
    public void findRemoteDevive(int remPort, String domainName, String password){
        mSvrDevice.FindDevInfo(0x00040000, remPort, domainName, password);
    }
    public void fetchDeviveInfo(){
        mSvrDevice.FindDevInfo(0x00080000, 0, "", "");
    }
    public void fetchWifiList(){
        mSvrDevice.fetchWifiList(gDevNumber);
    }
    public void modifyWifi(String wifiName, String password){
        mSvrDevice.modifyWifi(gDevNumber, wifiName, password);
    }
    public void modifyDevInfo(ADeviceInfo aDeviceInfo){
        mSvrDevice.modifyDevInfo(gDevNumber, aDeviceInfo);
    }
    public void debugControl(int debugUser, int layout, int mode, String path){
        mSvrDevice.debugControl(gDevNumber, debugUser, layout, mode, path);
    }
    public void readBulkFile(int type, int addrss, int length, String fileName){
//        mARdWrFileStatus.g4RuningTick = GetTickCount();
        mSvrDevice.readBulkFile(gDevNumber, type, addrss, length, fileName);
    }
    public void writeBulkFile(int type, int addrss, int length, String fileName){
//        mARdWrFileStatus.g4RuningTick = GetTickCount();
        mSvrDevice.writeBulkFile(gDevNumber, type, addrss, length, fileName);
    }
    public void writeRtcText(int code, String text){
        mSvrDevice.writeRtcText(gDevNumber, code, text);
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////
    public boolean isEventOnDestroy(){
        if ((mSvrDevice.g4EventMethod & DEVICE_EVENT_ON_DESTROY) > 0){
            return true;
        }
        return false;
    }
    public boolean isEventDbsDevInfo(){
        if ((mSvrDevice.g4EventMethod & DEVICE_EVENT_DBS_DEV_INFO) > 0){
            mSvrDevice.g4EventMethod &= ~DEVICE_EVENT_DBS_DEV_INFO;
            return true;
        }
        return false;
    }
    public boolean isEventFindDevInfo(){
        if ((mSvrDevice.g4EventMethod & DEVICE_EVENT_FIND_DEV_INFO) > 0){
            mSvrDevice.g4EventMethod &= ~DEVICE_EVENT_FIND_DEV_INFO;
            return true;
        }
        return false;
    }
    public boolean isEventFetchWifiList(){
        if ((mSvrDevice.g4EventMethod & DEVICE_EVENT_WIFI_LIST) > 0){
            mSvrDevice.g4EventMethod &= ~DEVICE_EVENT_WIFI_LIST;
            return true;
        }
        return false;
    }
    public boolean isEventLinkMode(){
        if ((mSvrDevice.g4EventMethod & DEVICE_EVENT_LINK_MODE) > 0){
            mSvrDevice.g4EventMethod &= ~DEVICE_EVENT_LINK_MODE;
            return true;
        }
        return false;
    }
    public boolean isEventDebugText(){
        if ((mSvrDevice.g4EventMethod & DEVICE_EVENT_DEBUG_TEXT) > 0){
            mSvrDevice.g4EventMethod &= ~DEVICE_EVENT_DEBUG_TEXT;
            return true;
        }
        return false;
    }
    public boolean isEventReadFile(){
        if ((mSvrDevice.g4EventMethod & DEVICE_EVENT_RD_FILE) > 0){
            mSvrDevice.g4EventMethod &= ~DEVICE_EVENT_RD_FILE;
            mSvrDevice.counterRunPercent(mARdWrFileStatus);
            return true;
        }
        return false;
    }
    public boolean isEventWriteFile(){
        if ((mSvrDevice.g4EventMethod & DEVICE_EVENT_WR_FILE) > 0){
            mSvrDevice.g4EventMethod &= ~DEVICE_EVENT_WR_FILE;
            mSvrDevice.counterRunPercent(mARdWrFileStatus);
            return true;
        }
        return false;
    }
    public boolean isEventBindInfo(){
        if ((mSvrDevice.g4EventMethod & DEVICE_EVENT_BIND_INFO) > 0){
            mSvrDevice.g4EventMethod &= ~DEVICE_EVENT_BIND_INFO;
            return true;
        }
        return false;
    }
    public boolean isEventShareAdmin(){
        if ((mSvrDevice.g4EventMethod & DEVICE_EVENT_SHARE_ADMIN) > 0){
            mSvrDevice.g4EventMethod &= ~DEVICE_EVENT_SHARE_ADMIN;
            return true;
        }
        return false;
    }
    public boolean isEventSysRegister(){
        if ((mSvrDevice.g4EventMethod & 0x00010000) > 0){
            mSvrDevice.g4EventMethod &= ~0x00010000;
            return true;
        }
        return false;
    }
    public boolean isEventAudioWork(){
        if ((mSvrDevice.g4EventMethod & 0x00020000) > 0){
            mSvrDevice.g4EventMethod &= ~0x00020000;
            return true;
        }
        return false;
    }
    public boolean isEventMediaWork(){
        if ((mSvrDevice.g4EventMethod & 0x00040000) > 0){
            mSvrDevice.g4EventMethod &= ~0x00040000;
            return true;
        }
        return false;
    }
    public boolean isEventAmplifyWork(){
        if ((mSvrDevice.g4EventMethod & 0x00080000) > 0){
            mSvrDevice.g4EventMethod &= ~0x00080000;
            return true;
        }
        return false;
    }

    ////////////////////////////////////////////////////////////////////////////////////////////////
    public void fetchBindInfo(){
        mSvrDevice.fetchBindInfo(gDevNumber);
    }
    public void fetchShareAdmin(){
        mSvrDevice.fetchShareAdmin(gDevNumber);
    }
    public void modifyBindInfo(int amount, ABindInfo aBindInfo[]){
        mSvrDevice.modifyBindInfo(gDevNumber, amount, aBindInfo);
    }
    public void modifyShareAdmin(int amount, AShareAdmin aShareAdmin[]){
        mSvrDevice.modifyShareAdmin(gDevNumber, amount, aShareAdmin);
    }




}
